package com.seagull.sb.dao.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.seagull.sb.dao.HomeWorkDAO;
import com.seagull.sb.domain.HomeWork;
import com.seagull.sb.domain.Teacher;
import com.seagull.sb.util.SBRequestContext;
import com.seagull.sb.util.SBUtil;

public class HomeWorkDAOImpl extends BaseDAO implements HomeWorkDAO {
	
	@Override
	public List<HomeWork> getHomeWork(SBRequestContext rc) throws Exception {
		Map<String,Object> qmap = new HashMap<String, Object>();
		
		qmap.put("org_id", new Long(rc.getOrgId()));
		qmap.put("class_id", new Long(rc.getClassId()));
		Object result = sqlMap.queryForList("homeWorkDB.getHomeWorkDetails",qmap);
		if(result instanceof List){
			return (List<HomeWork>)result;
		}
		return Collections.EMPTY_LIST;
	}
	
	@Override
	public List<HomeWork> getHomeWorkForTeacher(SBRequestContext rc) throws Exception {
		Map<String,Object> qmap = new HashMap<String, Object>();
		
		qmap.put("org_id", new Long(rc.getOrgId()));
		qmap.put("class_id", new Long(rc.getClassId()));
		Object result = sqlMap.queryForList("homeWorkDB.getHomeWorkDetailsTeacher",qmap);
		if(result instanceof List){
			return (List<HomeWork>)result;
		}
		return Collections.EMPTY_LIST;
	}

	@Override
	public boolean saveHomeWork(SBRequestContext rc, HomeWork homeWork)
			throws Exception {
		boolean result = false;
		
		Map<String,Object> qmap = new HashMap<String, Object>();

		qmap.put("class_id", new Long(homeWork.getClassId()));
		qmap.put("subject_id", homeWork.getSubjectId());
		qmap.put("target_date", homeWork.getTargetDt());
		qmap.put("details", homeWork.getDetails());

		// request context details
		qmap.put("org_id", new Long(rc.getOrgId()));
		qmap.put("created_by", rc.getTeacherId());
		qmap.put("created_dt", SBUtil.getSystemDate());
		
		Integer id = (Integer) sqlMap.insert("homeWorkDB.saveHomeWork",qmap);
		homeWork.setId(id.intValue());
		
		System.out.println("Home Work - Id Saved : "+id.toString());
		result = true;		
		
		return result;

	}	
	
}
